package com.ruoyi.web.controller.dict;

import java.util.List;
import javax.servlet.http.HttpServletResponse;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.PutMapping;
import org.springframework.web.bind.annotation.DeleteMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.dict.domain.TDictReportType;
import com.ruoyi.dict.service.ITDictReportTypeService;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

/**
 * 报修类型Controller
 * 
 * @author ruoyi
 * @date 2025-09-24
 */
@RestController
@RequestMapping("/system/report/type")
public class TDictReportTypeController extends BaseController
{
    @Autowired
    private ITDictReportTypeService tDictReportTypeService;

    /**
     * 查询报修类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:list')")
    @GetMapping("/list")
    public TableDataInfo list(TDictReportType tDictReportType)
    {
        startPage();
        List<TDictReportType> list = tDictReportTypeService.selectTDictReportTypeList(tDictReportType);
        return getDataTable(list);
    }

    /**
     * 导出报修类型列表
     */
    @PreAuthorize("@ss.hasPermi('system:type:export')")
    @Log(title = "报修类型", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, TDictReportType tDictReportType)
    {
        List<TDictReportType> list = tDictReportTypeService.selectTDictReportTypeList(tDictReportType);
        ExcelUtil<TDictReportType> util = new ExcelUtil<TDictReportType>(TDictReportType.class);
        util.exportExcel(response, list, "报修类型数据");
    }

    /**
     * 获取报修类型详细信息
     */
    @PreAuthorize("@ss.hasPermi('system:type:query')")
    @GetMapping(value = "/{code}")
    public AjaxResult getInfo(@PathVariable("code") String code)
    {
        return success(tDictReportTypeService.selectTDictReportTypeByCode(code));
    }

    /**
     * 新增报修类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:add')")
    @Log(title = "报修类型", businessType = BusinessType.INSERT)
    @PostMapping
    public AjaxResult add(@RequestBody TDictReportType tDictReportType)
    {
        // 检查编码是否已存在
        if (tDictReportTypeService.selectTDictReportTypeByCode(tDictReportType.getCode()) != null)
        {
            return error("编码已存在，请使用其他编码");
        }
        // 设置创建者
        tDictReportType.setCreatedBy(getUserId());
        return toAjax(tDictReportTypeService.insertTDictReportType(tDictReportType));
    }

    /**
     * 修改报修类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:edit')")
    @Log(title = "报修类型", businessType = BusinessType.UPDATE)
    @PutMapping
    public AjaxResult edit(@RequestBody TDictReportType tDictReportType)
    {
        // 检查编码是否已存在（排除当前记录）
        TDictReportType existingType = tDictReportTypeService.selectTDictReportTypeByCode(tDictReportType.getCode());
        if (existingType != null && !existingType.getCode().equals(tDictReportType.getCode()))
        {
            return error("编码已存在，请使用其他编码");
        }
        // 设置修改者
        tDictReportType.setUpdatedBy(getUserId());
        return toAjax(tDictReportTypeService.updateTDictReportType(tDictReportType));
    }

    /**
     * 删除报修类型
     */
    @PreAuthorize("@ss.hasPermi('system:type:remove')")
    @Log(title = "报修类型", businessType = BusinessType.DELETE)
	@DeleteMapping("/{codes}")
    public AjaxResult remove(@PathVariable String[] codes)
    {
        return toAjax(tDictReportTypeService.deleteTDictReportTypeByCodes(codes));
    }
}